package com.poul.acfun.ac_utils;

import android.util.Log;

import com.poul.acfun.ac_model.bean.PushBean;
import com.poul.acfun.ac_model.bean.WelcomeBean;
import com.poul.acfun.listener.OnGetListener;

import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by poul on 2016/10/19.
 */

public class AcNetUtil {
    private static AcServer acServer;
    private static String TAG = "AcNetUtil";

    public static void getWelcomImage(OnGetListener listener){
        getAPIServer().getStartImage()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<WelcomeBean>() {
                    @Override
                    public void onCompleted() {
                        Log.i(TAG, "欢迎界面加载完成");
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e(TAG, "onError: ====" + "欢迎见面图片加载有误" +e.toString());
                    }

                    @Override
                    public void onNext(WelcomeBean welcomeBean) {
                        listener.onNext(welcomeBean);
                    }
                });

    }

    public static void getPushList(OnGetListener listener){
        getAPIServer().getPush()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<PushBean>() {
                    @Override
                    public void onCompleted() {
                        Log.e("提示", "onCompleted: ====" + "数据获取成功");
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("提示", "onError: ====" + "申请网络数据失败"+e.getMessage());
                    }

                    @Override
                    public void onNext(PushBean pushBean) {
                        listener.onNext(pushBean);
                    }
                });
    }

    public static AcServer getAPIServer(){
        if (acServer == null) {
            String baseUrl = "http://api.aixifan.com/";
            acServer = getServer(baseUrl).create(AcServer.class);
        }
        return acServer;
    }

    private static Retrofit getServer(String url){
        return new Retrofit.Builder()
                .baseUrl(url)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
    }
}
